package com.wumai.keystrokes.gui;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.wumai.keystrokes.gui.events.IRefreshKeyGroupListPanelCallback;
import com.wumai.keystrokes.models.settings.KeyConfig;
import com.wumai.keystrokes.models.settings.KeyGroup;
import com.wumai.keystrokes.utils.KeyConfigManger;
import com.wumai.modernguilib.component.Button;
import com.wumai.modernguilib.panel.ScrollablePanel;
import com.wumai.modernguilib.panel.GridView;
import com.wumai.modernguilib.panel.WeightBasedView;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.util.text.StringTextComponent;

import javax.annotation.ParametersAreNonnullByDefault;


public class KeystrokesConfigScreen extends Screen implements IRefreshKeyGroupListPanelCallback {
    
    private Screen parentScreen;
    private KeyConfig selectedConfig;
    private KeyConfigManger keyConfigManger = null;

    // 使用GridView来管理四个区域
    private GridView mainGridView;
    
    // 界面状态：true表示显示配置详情，false表示显示配置列表
    
    // 按键捕获相关
    private boolean isCapturingKey = false;

    // 左边的调试信息
//    private TextLabel debugInfo;

    // 封装的右边列表
    private WeightBasedView rightPanel;
    private ScrollablePanel scrollableKeyGroupListPanel;
    private Button addKeyGroupButton;
    
    public KeystrokesConfigScreen(Screen parentScreen) {
        super(new StringTextComponent("Keystrokes Configuration"));
        this.parentScreen = parentScreen;
    }

    private void setInitVar() {
        // 初始化键的风格管理器
        keyConfigManger = KeyConfigManger.getInstance();
        keyConfigManger.loadConfig();

        // 创建主网格布局 (2x2)
        mainGridView = new GridView(2, 2);
        // 确保GridView填充整个屏幕，因为我们的控件库里面的控件并不是自身管理其样式，而是
        // 交给父容器进行管理，而这里的父容器不是我们自己定义的，所以这里要调用一个方法
        // 告诉这个容器没有父容器，于是填充整个屏幕就行
        mainGridView.fillParent();
        mainGridView.setHorizontalSpacing(0.005f);
        mainGridView.setVerticalSpacing(0.005f);

        
        // 设置列宽和行高
        // 左侧列占据大部分空间 这里也只是权重
        mainGridView.setColumnWidths(1.5f, 0.5f);
        // 上下行各占一半 权重
        mainGridView.setRowHeights(1.5f, 0.5f);

        // 调试信息
        // debugInfo = new TextLabel("Debug Info");

        // 右边按键组显示相关
        // 使用归一化坐标创建ScrollableListWidget
        rightPanel = new WeightBasedView(WeightBasedView.Orientation.VERTICAL);

        scrollableKeyGroupListPanel = new ScrollablePanel(
                0.06f,  // itemHeight (NDC坐标)
                0.0f    // itemSpacing (NDC坐标)
        );
        
        updateScrollableKeyGroupListPanel();

        addKeyGroupButton = new Button("添加") {
            @Override
            public void onClick() {
                System.out.println("Adding new key group...");
                // 添加按键组
                keyConfigManger.addKeyGroup("New Group");
                // 更新列表显示
                updateScrollableKeyGroupListPanel();
            }
        };
        rightPanel.addChild(scrollableKeyGroupListPanel, 0.9f);
        rightPanel.addChild(addKeyGroupButton, 0.1f);

        // mainGridView.addChild(debugInfo, 0, 0);
//        mainGridView.addChild(new Button("Test") {
//            @Override
//            public void onClick() {
//
//            }
//        }, 0, 0);
        mainGridView.addChild(rightPanel, 1, 0);
    }

    private void updateScrollableKeyGroupListPanel() {
        if (keyConfigManger == null) return;
        
        scrollableKeyGroupListPanel.clearItems();
        int i = 0;
        for (KeyGroup keyGroup : keyConfigManger.getKeyGroup()) {
            KeyGroupItem keyGroupItem =  new KeyGroupItem(keyGroup, i++, this);
            scrollableKeyGroupListPanel.addChild(keyGroupItem);
        }
    }

    private void switchToEditConfig(int index) {

    }

    @Override
    public void init() {
        this.buttons.clear();
        // 初始化窗口布局
        setInitVar();
    }
    
    @Override
    @ParametersAreNonnullByDefault
    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        this.renderBackground(matrixStack);
        // 渲染主网格布局
        mainGridView.render(matrixStack, mouseX, mouseY, partialTicks);
        super.render(matrixStack, mouseX, mouseY, partialTicks);
    }

    private int getCurrentScreenHeight() {
        return Minecraft.getInstance().getWindow().getGuiScaledHeight();
    }

    private int getCurrentScreenWidth() {
        return Minecraft.getInstance().getWindow().getGuiScaledWidth();
    }

    @Override
    public boolean mouseClicked(double mouseX, double mouseY, int button) {
        return mainGridView.mouseClicked(mouseX, mouseY, button);
    }

    @Override
    public boolean mouseReleased(double mouseX, double mouseY, int button) {
        // 将事件传递给主网格布局，它会自动处理子组件的事件
        return mainGridView.mouseReleased(mouseX, mouseY, button);
    }
    
    @Override
    public boolean mouseDragged(double mouseX, double mouseY, int button, double dragX, double dragY) {
        // 将事件传递给主网格布局，它会自动处理子组件的事件
        return mainGridView.mouseDragged(mouseX, mouseY, button, dragX, dragY);
    }

    @Override
    public boolean mouseScrolled(double mouseX, double mouseY, double delta) {
        // 将事件传递给主网格布局，它会自动处理子组件的事件
        return mainGridView.mouseScrolled(mouseX, mouseY, delta);
    }
    
    private void saveConfig() {
        keyConfigManger.saveConfig();
    }
    
    @Override
    public void onClose() {
        // 关闭界面时保存配置
        saveConfig();
        Minecraft.getInstance().setScreen(parentScreen);
    }

    @Override
    public void refreshKeyGroupList() {
        this.updateScrollableKeyGroupListPanel();
    }
}